package com.songtech.uitest.module.propose;

import com.songtech.uitest.entities.Indent;
import com.songtech.uitest.module.common.MenuHandler;
import com.songtech.uitest.module.common.TabHandler;
import com.songtech.uitest.util.Space;
import com.songtech.uitest.util.StepParser;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

@Component
public class GetTowerId {

    @Autowired
    @Qualifier("indent")
    private Indent indent;

    @Autowired
    private TabHandler tabHandler;

    @Autowired
    private MenuHandler menuHandler;

    @Autowired
    private StepParser stepParser;

    @Autowired
    private GenerateTowerId generateTowerId;

    private Logger logger = LoggerFactory.getLogger(GetTowerId.class);

    //打印日志前的缩进空格
    @Autowired
    private Space space;


    public String getTowerId(String step) throws Exception{

        WebDriver driver = stepParser.parseDriver(step);


        tabHandler.closeAllTabs(driver);
        //回到需求提出页获取铁塔侧订单号
        menuHandler.clickMenuItem(driver, "铁塔建设", "需求提出", "");

        //到页面上抓取铁塔侧订单号
        driver.switchTo().frame("iframeundefined");
        Thread.sleep(6000);

        //查询此单
        //待办状态设为所有
        Select taskStatus = new Select(driver.findElement(By.id("taskStatus")));
        taskStatus.selectByIndex(0);
        //输入单号查询
        driver.findElement(By.id("keywords")).sendKeys(indent.getMobileId());
        driver.findElement(By.xpath("//button[text()='查询']")).click();
        Thread.sleep(5000);

        //先试一次
        String towerId = driver.findElement(By.xpath("//*[@id='tb']/tbody/tr/td[7]")).getText();
        if(!towerId.equals("-")){
            indent.setTowerId(towerId);
            logger.info(space.getSpace() + "获取铁塔单号成功，铁塔单号为：" + indent.getTowerId());
            tabHandler.closeAllTabs(driver);
            return towerId;
        }

        WebElement td = null;
        towerId = "";

        //循环等待获取铁塔单号
        Thread.sleep(1*60*1000);

        int count = 0;
        do {
            Thread.sleep(30000);
            driver.findElement(By.xpath("//button[text()='查询']")).click();
            Thread.sleep(5000);
            td = driver.findElement(By.xpath("//*[@id='tb']/tbody/tr/td[7]"));
            towerId = td.getText();
            count++;
        }
        //如果一直是-，且没有变红，且在10次以内(5~6分钟)
        while (towerId.equals("-") && !td.getAttribute("style").contains("color") && count < 10);

        if(count < 10 && !td.getAttribute("style").contains("color")) {
            indent.setTowerId(towerId);
        }
        else {
            towerId = generateTowerId.generateTowerId("00");
            //将单子的流程位置回退一格进行抵消
            indent.setStepPosition(indent.getStepPosition() - 1);
        }
        logger.info(space.getSpace() + "获取铁塔单号成功，铁塔单号为：" + indent.getTowerId());

        //记录单子的流程进行位置
        indent.setStepPosition(indent.getStepPosition() + 1);

        //关闭此页
        tabHandler.closeAllTabs(driver);

        return towerId;
    }


}
